using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.DataAccessProvider;
using DTN.Core;
namespace com.dtn.deals
{
	public class tbl_OrderManager : Itbl_OrderManager
	{
		private static Itbl_OrderManager itbl_OrderManager = null;
		private tbl_OrderManager()
		{
		}
		public static Itbl_OrderManager CreateInstance()
		{
			if (itbl_OrderManager == null)
			{
				itbl_OrderManager = new tbl_OrderManager();
			}
			return itbl_OrderManager;
		}
		public int Inserttbl_Order(tbl_Order tbl_order)
		{
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@OrderId", SqlDbType.Int),
																new SqlParameter("@ProductId", SqlDbType.Int),
																new SqlParameter("@ProductName", SqlDbType.NVarChar),
																new SqlParameter("@SaleOff", SqlDbType.NVarChar),
																new SqlParameter("@Price", SqlDbType.Float),
																new SqlParameter("@Quantity", SqlDbType.Int),
																new SqlParameter("@Total", SqlDbType.Float),
																new SqlParameter("@OrderDate", SqlDbType.DateTime),
																new SqlParameter("@MemberId", SqlDbType.Int),
																new SqlParameter("@TypeTransaction", SqlDbType.NVarChar),
																new SqlParameter("@OrderStatus", SqlDbType.NVarChar),
																new SqlParameter("@PartnerId", SqlDbType.Int),
																new SqlParameter("@OrderPhone", SqlDbType.NVarChar),
																new SqlParameter("@OrderGuestName", SqlDbType.NVarChar),
																new SqlParameter("@OrderAddress", SqlDbType.NVarChar),
                                                                new SqlParameter("@CodeOrder", SqlDbType.VarChar),
															};
			parameters[0].Direction = ParameterDirection.Output;
			parameters[1].Value = tbl_order.ProductId;
			parameters[2].Value = tbl_order.ProductName;
			parameters[3].Value = tbl_order.SaleOff;
			parameters[4].Value = tbl_order.Price;
			parameters[5].Value = tbl_order.Quantity;
			parameters[6].Value = tbl_order.Total;
			parameters[7].Value = tbl_order.OrderDate;
			parameters[8].Value = tbl_order.MemberId;
			parameters[9].Value = tbl_order.TypeTransaction;
			parameters[10].Value = tbl_order.OrderStatus;
			parameters[11].Value = tbl_order.PartnerId;
			parameters[12].Value = tbl_order.OrderPhone;
			parameters[13].Value = tbl_order.OrderGuestName;
			parameters[14].Value = tbl_order.OrderAddress;
            parameters[15].Value = tbl_order.CodeOrder;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Inserttbl_Order", CommandType.StoredProcedure, parameters);
				tbl_order.OrderId = Convert.ToInt32(parameters[0].Value);
			}
			catch (Exception ex)
			{
				tbl_order.OrderId = -1;
			}
			return tbl_order.OrderId;
		}
		public bool Updatetbl_Order(tbl_Order tbl_order)
		{
			bool bresult = false;
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@OrderId", SqlDbType.Int),
																new SqlParameter("@ProductId", SqlDbType.Int),
																new SqlParameter("@ProductName", SqlDbType.NVarChar),
																new SqlParameter("@SaleOff", SqlDbType.NVarChar),
																new SqlParameter("@Price", SqlDbType.Float),
																new SqlParameter("@Quantity", SqlDbType.Int),
																new SqlParameter("@Total", SqlDbType.Float),
																new SqlParameter("@OrderDate", SqlDbType.DateTime),
																new SqlParameter("@MemberId", SqlDbType.Int),
																new SqlParameter("@TypeTransaction", SqlDbType.NVarChar),
																new SqlParameter("@OrderStatus", SqlDbType.NVarChar),
																new SqlParameter("@PartnerId", SqlDbType.Int),
																new SqlParameter("@OrderPhone", SqlDbType.NVarChar),
																new SqlParameter("@OrderGuestName", SqlDbType.NVarChar),
																new SqlParameter("@OrderAddress", SqlDbType.NVarChar),
															};
			parameters[0].Value = tbl_order.OrderId;
			parameters[1].Value = tbl_order.ProductId;
			parameters[2].Value = tbl_order.ProductName;
			parameters[3].Value = tbl_order.SaleOff;
			parameters[4].Value = tbl_order.Price;
			parameters[5].Value = tbl_order.Quantity;
			parameters[6].Value = tbl_order.Total;
			parameters[7].Value = tbl_order.OrderDate;
			parameters[8].Value = tbl_order.MemberId;
			parameters[9].Value = tbl_order.TypeTransaction;
			parameters[10].Value = tbl_order.OrderStatus;
			parameters[11].Value = tbl_order.PartnerId;
			parameters[12].Value = tbl_order.OrderPhone;
			parameters[13].Value = tbl_order.OrderGuestName;
			parameters[14].Value = tbl_order.OrderAddress;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Updatetbl_Order", CommandType.StoredProcedure, parameters);
				bresult = true;
			}
			catch (Exception ex)
			{
				bresult = false;
			}
			return bresult;
		}
        public bool UpdatetblOrder_CodeOrder(int ID, string CodeOrder) 
        {
            bool bresult = false;
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            int inumOfRowEffect = 0;
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@OrderId", SqlDbType.Int),
																new SqlParameter("@CodeOrder", SqlDbType.VarChar),
																
															};
            parameters[0].Value = ID;
            parameters[1].Value = CodeOrder;
            try
            {
                inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_UpdatetblOrder_CodeOrder", CommandType.StoredProcedure, parameters);
                bresult = true;
            }
            catch (Exception ex)
            {
                bresult = false;
            }
            return bresult;
        }
		public bool Deletetbl_Order(int id)
		{
			bool bresult = false;
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@OrderId", SqlDbType.Int),
															};
			parameters[0].Value = id;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Deletetbl_Order", CommandType.StoredProcedure, parameters);
				bresult = true;
			}
			catch (Exception ex)
			{
				bresult = false;
			}
			return bresult;
		}
		public tbl_Order Selecttbl_OrderByID(int id)
		{
			IDataReader dr;
			tbl_Order tbl_order = new tbl_Order();
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@OrderId", SqlDbType.Int),
															};
			parameters[0].Value = id;
			try
			{
				dr = dataProvider.ExecuteReader("sp_Selecttbl_Order", CommandType.StoredProcedure, parameters);
				while(dr.Read())
				{
					tbl_order.OrderId = Convert.ToInt32(dr["OrderId"].ToString());
					tbl_order.ProductId = Convert.ToInt32(dr["ProductId"].ToString());
					tbl_order.ProductName = (dr["ProductName"].ToString());
					tbl_order.SaleOff = (dr["SaleOff"].ToString());
					tbl_order.Price = Convert.ToDouble(dr["Price"].ToString());
					tbl_order.Quantity = Convert.ToInt32(dr["Quantity"].ToString());
					tbl_order.Total = Convert.ToDouble(dr["Total"].ToString());
					tbl_order.OrderDate = Convert.ToDateTime(dr["OrderDate"].ToString());
					tbl_order.MemberId = Convert.ToInt32(dr["MemberId"].ToString());
					tbl_order.TypeTransaction = (dr["TypeTransaction"].ToString());
					tbl_order.OrderStatus = (dr["OrderStatus"].ToString());
					tbl_order.PartnerId = Convert.ToInt32(dr["PartnerId"].ToString());
					tbl_order.OrderPhone = (dr["OrderPhone"].ToString());
					tbl_order.OrderGuestName = (dr["OrderGuestName"].ToString());
					tbl_order.OrderAddress = (dr["OrderAddress"].ToString());
				}

			}
			catch (Exception ex)
			{
				return null;
			}
			return tbl_order;
		}
		public DataTable Selecttbl_OrdersDynamic(string Where, string Order)
		{
			DataSet ds = new DataSet();
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500),
																new SqlParameter("@OrderByExpression", SqlDbType.NVarChar,250),
															};
			parameters[0].Value = Where;
			parameters[1].Value = Order;
			try
			{
				ds = dataProvider.ExecuteDataSet("sp_Selecttbl_OrdersDynamic", CommandType.StoredProcedure, parameters);
			}
			catch (Exception ex)
			{
				return null;
			}
			return ds.Tables["Table"];
		}

        public DataTable Selecttbl_OrdersPageDynamic(string Where, int StartIndex, int Maxrecords) 
        {
            DataSet ds = new DataSet();
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500){Value = Where},
																new SqlParameter("@Startindex", SqlDbType.Int) {Value = StartIndex},
                                                                new SqlParameter("@Maxrecords", SqlDbType.Int) {Value = Maxrecords}
															};
            try
            {
                ds = dataProvider.ExecuteDataSet("sp_Selecttbl_OrdersPagesDynamic", CommandType.StoredProcedure, parameters);
            }
            catch (Exception ex)
            {
                return null;
            }
            return ds.Tables["Table"];
        }

        public DataTable Selecttbl_ReportPatnerOrders(string Where, int StartIndex, int Maxrecords) 
        {
            DataSet ds = new DataSet();
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500){Value = Where},
																new SqlParameter("@Startindex", SqlDbType.Int) {Value = StartIndex},
                                                                new SqlParameter("@Maxrecords", SqlDbType.Int) {Value = Maxrecords}
															};
            try
            {
                ds = dataProvider.ExecuteDataSet("sp_Selecttbl_PatnerOrders", CommandType.StoredProcedure, parameters);
            }
            catch (Exception ex)
            {
                return null;
            }
            return ds.Tables["Table"];
        }
	}
}
